#!/bin/bash
set -e

if [[ -n "${RF_DEBUG}" ]]; then
    set -x
fi

source "scripts/update"

DIR="$(dirname "$0")"

function usage() {

    echo -n \
"Usage: $(basename "$0") [--download]

Load the database dump located at data/database.pgdump if present.
Otherwise, downloads the dump from S3. If the download flag is used, ignores
and overwrites the existing dump.
"
}

function create_database_backup() {
    if [ -f data/database.pgdump ]; then
        echo "Renaming old database dump"
        HASH=$(date +%N)
        mv "data/database.pgdump" "data/database.pgdump.${HASH}"
    fi
    echo "Creating new dev database dump"
    docker-compose exec -T postgres pg_dump -U rasterfoundry -Fc -f /tmp/data/database.pgdump
}

function upload_database_backup() {
    if [ ! -f data/database.pgdump ]; then
        logInfo "No databaes dump found"
    else
        # create backup of old dump
        HASH=$(date +'%N')
        aws s3 mv --sse AES256 "s3://${RF_SETTINGS_BUCKET}/database.pgdump" "s3://${RF_SETTINGS_BUCKET}/databaseArchives/database.pgdump.${HASH}"
        # upload new dump
        aws s3 cp --sse AES256 "data/database.pgdump" "s3://${RF_SETTINGS_BUCKET}/database.pgdump"
    fi
}

function download_database_backup() {

    pushd "${DIR}/.."
    echo "Downloading database from s3"
    aws s3 cp "s3://${RF_SETTINGS_BUCKET}/database.pgdump" "data/database.pgdump"
    popd
}

function download_development_images() {

    pushd "${DIR}/.."
    echo "Downloading images from s3"
    aws s3 sync "s3://${RF_SETTINGS_BUCKET}/development-images/" "data/"
    popd
}

function load_database_backup() {
    echo "Drop rasterfoundry database"
    docker-compose \
        exec -T postgres dropdb -U rasterfoundry rasterfoundry

    echo "Create rasterfoundry database"
    docker-compose \
        exec -T postgres createdb -U rasterfoundry rasterfoundry

    echo "Restore database from backup"
    # Command to create database backup
    # gosu postgres pg_dump -Fc rasterfoundry > database.pgdump
    docker-compose \
        exec -T postgres pg_restore -U rasterfoundry -Fc -d rasterfoundry /tmp/data/database.pgdump
    ./scripts/migrate migrate
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]
then
    if [ "${1:-}" = "--help" ]
    then
        usage
    else
        if [ "${1:-}" = "--create" ]; then
            create_database_backup
        elif [ "${1:-}" = "--upload" ]; then
            upload_database_backup
        else
            if [ "${1:-}" = "--download" ] || [ ! -f data/database.pgdump ]; then
                download_database_backup
                download_development_images
            fi

            # API server won't start until Postgres is passing Health Checks
            docker-compose \
                -f "${DIR}/../docker-compose.yml" \
                -f "${DIR}/../docker-compose.test.yml" \
                up -d api-server-test

            docker-compose \
                -f "${DIR}/../docker-compose.yml" \
                -f "${DIR}/../docker-compose.test.yml" \
                rm -sf api-server-test

            load_database_backup
        fi
    fi
    exit
fi
